package com.bwie.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.SectionMapper;
import com.bwie.mapper.UserMapper;
import com.bwie.pojo.Member;
import com.bwie.pojo.Section;
import com.bwie.pojo.User;
import com.bwie.service.IUserService;
import com.bwie.service.MemberService;
import com.bwie.mapper.MemberMapper;
import com.bwie.service.UserService;
import com.bwie.utils.R;
import com.bwie.vo.MemberVo;
import com.bwie.vo.UserVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
* @author 61630
* @description 针对表【tb_member】的数据库操作Service实现
* @createDate 2024-07-30 10:09:21
*/
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member>
    implements MemberService{
    @Resource
    IUserService iUserService;
    @Resource
    private MemberMapper memberMapper;
    @Resource
    private SectionMapper sectionMapper;
    @Resource
    private UserMapper userMapper;
    @Override
    public R getList(MemberVo memberVo) {
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        if(!memberVo.getMemberNum().equals("")){
            wrapper.lambda().eq(Member::getMemberNum,memberVo.getMemberNum());
        }
        if(!memberVo.getMemberName().equals("")){
            wrapper.lambda().like(Member::getMemberName,memberVo.getMemberName());
        }
        if(Objects.nonNull(memberVo.getMemberId())){
            wrapper.lambda().like(Member::getMemberId,memberVo.getMemberId());
        }
        Page<Member> page = new Page<>(memberVo.getPageNum(),memberVo.getPageSize());


        Page<Member> pages = memberMapper.selectPage(page, wrapper);
        List<Member> records = pages.getRecords();
        List<Section> sections = sectionMapper.selectList(null);
        List<User> users = userMapper.selectList(null);
        for (Member member : records) {
            member.setMemberNum(member.getMemberNum().substring(0,2)+"******"+member.getMemberNum().substring(9,10));
            for (Section section : sections) {
                if (member.getSectionId() == section.getId()) {
                    member.setSection(section);
                }
            }
            for (User user : users) {
                if (member.getUserId() == user.getId()) {
                    member.setUser(user);
                }
            }
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("pages",pages.getPages());
        map.put("total",pages.getTotal());
        map.put("list",records);
        return R.success(map);
    }
}




